package com.lv.phuotbacnam.bl.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * Created by VanQuan on 30/08/2014.
 */
public class ImageLoaderCus {

    /* use to display image from url */
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    public ImageLoaderCus(Context context){
        /* To init image loader */
        imageLoader = ImageLoader.getInstance();
        // Initialize ImageLoader with configuration. Do it once.
        //imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        // Init ImageLoader
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .discCache(new UnlimitedDiscCache(context.getExternalCacheDir()))
                .discCacheSize(20 * 50 * 50)//20Mb
                .build();
        imageLoader.init(config);
        options = new DisplayImageOptions.Builder()
                .cacheOnDisc(true)
                .cacheInMemory(false)
                .build();
    }

    public void showImage(ImageView img, String url){
        imageLoader.displayImage(url,img,options);
    }

    public void showImage(ImageView img, String url,final CallBackFromParse<String,String> resultCallBack){
       imageLoader.displayImage(url,img,options,new ImageLoadingListener() {
           @Override
           public void onLoadingStarted(String s, View view) {

           }

           @Override
           public void onLoadingFailed(String s, View view, FailReason failReason) {
               resultCallBack.done("","");
           }

           @Override
           public void onLoadingComplete(String s, View view, Bitmap bitmap) {
               resultCallBack.done("","");
           }

           @Override
           public void onLoadingCancelled(String s, View view) {
               resultCallBack.done("","");
           }
       });
    }
}
